//
//  CJTabBarController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-04-19.
//

import UIKit

open class CJTabBarController: UITabBarController {

    public var cj_tabTintColor: UIColor?
    public var cj_tabUnSelColor: UIColor?
    public var cj_tabSelColor: UIColor?
    
    open override func viewDidLoad() {
        super.viewDidLoad()

        view.cj_didSkin = { [weak self] kit in
            self?.cj_skinAction(kit)
        }
    }
    
    /// 全局项目刷新回调(子类继承实现)
    open func cj_skinAction(_ kit: Any) {
        setupTabBarDefaultApperance()
    }
    
    /// 更新Tab栏颜色
    open func setupTabBarDefaultApperance() {
        let tintColor = cj_tabTintColor ?? GV_CJFrame_tabTintColor
        let unSelColor = cj_tabUnSelColor ?? GV_CJFrame_tabUnSelColor
        let selColor = cj_tabSelColor ?? GV_CJFrame_tabSelColor
        setupTabBarAppearance(tintColor: tintColor, unSelColor: unSelColor, selColor: selColor)
    }
    
    /// 更新Tab栏颜色
    open func setupTabBarAppearance(tintColor: UIColor = .white,
                                    unSelColor: UIColor = .gray,
                                    selColor: UIColor = .black) {
        if #available(iOS 13.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithTransparentBackground()
            appearance.shadowColor = .clear
            appearance.backgroundColor = tintColor
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unSelColor]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selColor]
            self.tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                self.tabBar.scrollEdgeAppearance = appearance
            }
        } else {
            self.tabBar.isTranslucent = true
            self.tabBar.backgroundColor = tintColor
            self.tabBar.unselectedItemTintColor = unSelColor
            self.tabBar.tintColor = selColor
            
            self.tabBar.shadowImage = UIImage()
            self.tabBar.backgroundImage = UIImage()
        }
    }
}
